package com.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.config.IdWorker;
import com.ht.domain.bo.MyDoctorBO;
import com.ht.domain.po.*;
import com.ht.mapper.*;
import com.ht.service.MyDoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class MyDoctorServiceImpl implements MyDoctorService {
    @Autowired
    private MyDoctorMapper myDoctorMapper;

    @Autowired
    private TbDoctorMapper tbDoctorMapper;

    @Autowired
    private TbLevelMapper tbLevelMapper;

    @Autowired
    private TbHospitalMapper tbHospitalMapper;

    @Autowired
    private TbDepartmentMapper tbDepartmentMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public MyDoctorBO serviceDetails(Long id, Integer price, Integer time) {
        QueryWrapper q = new QueryWrapper();
        q.eq("id",id);
        TbDoctor po = tbDoctorMapper.selectOne(q);
        MyDoctorBO bo = new MyDoctorBO();
        bo.setId(idWorker.nextId());
        bo.setDoctorId(po.getId());
        bo.setDoctorName(po.getDoctorName());
        //根据医院id查询医院名称
        QueryWrapper<TbHospital> hq = new QueryWrapper<>();
        hq.eq("id",po.getHospitalId());
        TbHospital tbHospital = tbHospitalMapper.selectOne(hq);
        bo.setHospitalId(bo.getHospitalId());
        bo.setHospitalName(tbHospital.getHospitalName());
        //根据level查询药师的职位
        QueryWrapper<TbLevel> lq = new QueryWrapper<>();
        lq.eq("id",po.getLevel());
        TbLevel tbLevel = tbLevelMapper.selectOne(lq);
        bo.setLevel(po.getLevel());
        bo.setLevelName(tbLevel.getLevel());
        //根据科室id查询科室
        QueryWrapper dp = new QueryWrapper();
        dp.eq("id",po.getDepartmentId());
        TbDepartment tbDepartment = tbDepartmentMapper.selectOne(dp);
        bo.setDepartmentId(po.getDepartmentId());
        bo.setDepartmentName(tbDepartment.getDepartmentName());
        //查询当前医生是多少患者的私人医生
        QueryWrapper mq = new QueryWrapper();
        mq.eq("doctor_id",id);
        List list = myDoctorMapper.selectList(mq);
        int i = list.size();
        bo.setNum(i);
        bo.setPrivateDoctor("1");
        LocalDate date = LocalDate.now();
        LocalDate date1 = date.plusMonths(time);
        Date from = Date.from(date1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
        bo.setSeverTime(from);
        MyDoctor md = new MyDoctor();
        md.setId(bo.getId());
        md.setDoctorId(bo.getDoctorId());
        md.setUserId(1111111L);
        md.setSeverTime(from);
        md.setPrivateDoctor("1");
        md.setStatus("0");
        int insert = myDoctorMapper.insert(md);
        return bo;
    }
}
